﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WPS.Log;
using WPS.SDK.exception;
using WPS.SDK.http;
using WPS.SDK.model.oauth;

namespace WPS.SDK.openapi
{
    public class OAuth2
    {
        /// <summary>
        /// 通过code获取access_token
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Token getToken(string code)
        {
            try
            {
                string url = Config.getOpenApiServer() + "/oauthapi/v2/token?appid=" + Config.getAppid() + "&appkey=" + Config.getAppKey() + "&code=" + code;
                Token token = Token.fromJsonObject(new Request().Get(url).getJSONObject());
                return token;
            }
            catch (YunException ex)
            {
                LogHelper.Error("getToken：" + ex.Message + "<br/>" + ex.StackTrace);
                return null;
            }
        }

        /// <summary>
        /// 通过code获取access_token
        /// </summary>
        /// <param name="code"></param>
        /// <param name="appApi"></param>
        /// <param name="appKey"></param>
        /// <returns></returns>
        public static Token getToken(string appApi, string appKey, string code)
        {
            try
            {
                string url = Config.getOpenApiServer() + "/oauthapi/v2/token?appid=" + appApi + "&appkey=" + appKey + "&code=" + code;
                LogHelper.Info(url);
               Token token = Token.fromJsonObject(new Request().Get(url).getJSONObject());
                return token;
            }
            catch (YunException ex)
            {
                LogHelper.Error("getToken：" + ex.Message + "<br/>" + ex.StackTrace);
            }
            return null;
        }

        /// <summary>
        /// 刷新access_token (如果需要)
        /// </summary>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        public static Token refreshAccessToken(string refreshToken)
        {
            JObject jsonObject = new JObject();
            jsonObject.Add("appid", Config.getAppid());
            jsonObject.Add("appkey", Config.getAppKey());
            jsonObject.Add("refresh_token", refreshToken);
            string jsonString = JsonConvert.SerializeObject(jsonObject);
            string url = Config.getOpenApiServer() + "/oauthapi/v2/token/refresh";
            Token token = Token.fromJsonObject(new Request().Post(url, jsonString).getJSONObject());
            return token;
        }

        /// <summary>
        /// 拉取用户信息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static UserInfo getUserInfo(string accessToken, string openId)
        {
            string url = Config.getOpenApiServer() + "/oauthapi/v2/user?appid=" + Config.getAppid() + "&access_token=" + accessToken + "&openid=" + openId;
            return UserInfo.fromJsonObject(new Request().Get(url).getJSONObject());
        }

    }
}

